package com.my.content.liveproject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

import com.my.content.leliving.LeLiveMainActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView ImgLive;
    private ImageView ImgMe;
    private ImageView ImgRoom;
    private MainFragment mainFragment;
    private MineFragment mineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImgLive = (ImageView) findViewById(R.id.main_live);//主页图片按钮
        ImgMe = (ImageView) findViewById(R.id.main_me);//我图片按钮
        ImgRoom = (ImageView) findViewById(R.id.main_room);//直播按钮
        //设置点击事件监听
        findViewById(R.id.main_live).setOnClickListener(this);
        findViewById(R.id.main_me).setOnClickListener(this);
        findViewById(R.id.main_room).setOnClickListener(this);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        mainFragment = new MainFragment();
        mineFragment = new MineFragment();
        transaction.add(R.id.main_rl, mainFragment);
        transaction.commit();
    }

    //图片的点击监听
    @Override
    public void onClick(View view) {
        //碎片的事务开启
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (view.getId()) {
//            case R.id.main_search:
//                //android:state_pressed 是否按下，如一个按钮触摸或者点击。
//                //setPressed，使state_pressed属性生效
//                ImgSearch.setPressed(true);
//                break;
            //点击主页，跳转到主页
            case R.id.main_live:
                //判断显示个人页面时
                if (mainFragment.isHidden() && mineFragment.isAdded()) {
                    transaction.hide(mineFragment).show(mainFragment);
                    //判断此时处于主页面时候点击触发刷新
                    //!mineFragment.isAdded()条件是为了解决刚进程序时候点击主页面出现执行
                    // else内容的命令的bug
                } else if (mainFragment.isAdded() && mineFragment.isHidden()||!mineFragment.isAdded()) {
                    Toast.makeText(this, "fresh", Toast.LENGTH_SHORT).show();
                } else {
                    transaction.hide(mainFragment).show(mineFragment);
                }
                break;
            //点击我的页面，跳到相关页面
            case R.id.main_me:
                //判断主页添加和个人页面添加来达到切换页面或者留在原页面的目的
                if (mainFragment.isAdded() && mineFragment.isHidden() || mineFragment.isAdded() && mainFragment.isHidden()) {
                   //隐藏主页显示个人页面
                    transaction.hide(mainFragment).show(mineFragment);
                } else {
                    //以上条件不满足时，说明个人页面还没有添加，所以隐藏主页面的碎片，添加个人页面的碎片
                    transaction.hide(mainFragment).add(R.id.main_rl, mineFragment);
                }
                break;
            case R.id.main_room:
                ImgRoom.setPressed(true);
                startActivity(new Intent(MainActivity.this, LeLiveMainActivity.class));
                break;
        }
        //提交事务
        transaction.commit();
    }
}
